package main;

import java.util.*;

public class Library {
    private String name;
    private List<Book> books = new ArrayList<Book>();
    private List<Abonement> abonements = new ArrayList<Abonement>();

    public Library(String name) {
        this.name = name;
    }

    private int indexOfAbonement(String name) throws LibraryException {
        int result = 0;
        for (Abonement abonement : abonements) {
            if (abonement.getName().equals(name)) {
                return result;
            }
            result++;
        }

        throw new LibraryException("No such Abonement in the Library");
    }


    public Abonement getAbonement(String name) throws LibraryException {
        return abonements.get(indexOfAbonement(name));
    }

    private int indexOfBook(String name) throws LibraryException {
        int result = 0;
        for (Book book : books) {
            if (book.getName().equals(name)) {
                return result;
            }
            result++;
        }

        throw new LibraryException("No such Abonement in the Library");
    }

    public Book getBook(String name) throws LibraryException {
        return books.get(indexOfBook(name));
    }

    public void addBook(Book book) {
        books.add(book);
    }

    public String getAbonements() {
        StringBuilder result = new StringBuilder();
        for (Abonement abonement : abonements) {
            result.append(abonement + ": " + abonement.getCurrentBooks() + "\n");
        }

        return result.toString();
    }

    public void addAbonement(Abonement abonement) {
        abonements.add(abonement);
    }

    public void removeAbonement(String name) throws LibraryException {
        abonements.remove(indexOfAbonement(name));
    }

    public void removeBook(String name) throws LibraryException {
        books.remove(indexOfBook(name));
    }

}
